package com.echoesnet.echoesshortvideo.views.adapters;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.support.v4.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.Beans.IncludeNavBarBean;
import com.echoesnet.echoesshortvideo.Beans.Video;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.Image.GlideApp;
import com.echoesnet.echoesshortvideo.Utils.SharePreUtils;
import com.echoesnet.echoesshortvideo.databinding.VideoPlayBinding;
import com.echoesnet.echoesshortvideo.views.LikeHeartLayout;
import com.flipboard.bottomsheet.BottomSheetLayout;
import com.flipboard.bottomsheet.OnSheetDismissedListener;
import com.like.LikeButton;
import com.like.OnLikeListener;
import com.orhanobut.logger.Logger;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import am.util.viewpager.adapter.RecyclePagerAdapter;

/**
 * @author Administrator
 * @Date 2018/4/27
 * @Version 1.0
 */
public class VerticalViewPagerAdapter2 extends RecyclePagerAdapter<MyPagerViewHolder>// FragmentPagerAdapter//FragmentStatePagerAdapter//
{
    private final String TAG = VerticalViewPagerAdapter2.class.getSimpleName();

    private Map<Integer, Map<String, Object>> videoMap;

    private Activity mActivity;
    private List<Video> mlist;
    private boolean isLiked;
    private MyPagerViewHolder viewHolder;

    private boolean isPrepared = false;

    private ImageView btnPlay;

    private VideoPlayBinding binding;

    private int playIndex;

    private VerticalViewPagerAdapterCallBack verticalViewPagerAdapterCallBack;

    public VerticalViewPagerAdapter2(Activity mActivity, List<Video> mlist, int playIndex)
    {
        videoMap = new ArrayMap<>();
        this.mActivity = mActivity;
        this.mlist = mlist;
        this.playIndex = playIndex;
    }

    @Override
    public int getItemCount()
    {
        return mlist.size();
    }

    @Override
    public int getItemViewType(int position)
    {
        return super.getItemViewType(position);
    }

    public boolean isPrepared()
    {
        return isPrepared;
    }

    @Override
    public MyPagerViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        Logger.t(TAG).d("viewPager onCreateViewHolder");
//        View view = LayoutInflater.from(mActivity).inflate(R.layout.video_play, parent, false);
        VideoPlayBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.video_play,
                parent,
                false);

        MyPagerViewHolder myPagerViewHolder = new MyPagerViewHolder(binding.getRoot());
        myPagerViewHolder.setBinding(binding);
        return myPagerViewHolder;
    }

    @Override
    public void onBindViewHolder(MyPagerViewHolder holder, int position)
    {
        Logger.t(TAG).d("onBindViewHolder>>>>>>>>>>>>>>>>>>>>>>>" + position);
//        TinyVideoInfoEntity videoInfoEntity = mlist.get(position);
        VideoPlayBinding binding = (VideoPlayBinding) holder.getBinding();
        this.binding = binding;
        Video videoInfoEntity = mlist.get(position);

        IncludeNavBarBean navBean = new IncludeNavBarBean(false, true);
        binding.relNavBar.setNavBean(navBean);
        binding.relNavBar.itvLeft.setOnClickListener(view -> mActivity.finish());
        String videoUrl = videoInfoEntity.getUrl();
        Map<String, Object> map = new HashMap<>();
        map.put("image", binding.btnPlay);
        map.put("video", binding.playVideo);
        videoMap.put(position, map);
//        holder.playVideo.setShowPauseCover(true);
        binding.playVideo.setUp(videoUrl, true, null);
        binding.playVideo.setStartAfterPrepared(true);
        binding.playVideo.setVideoAllCallBack(new GSYSampleCallBack()
        {
            @Override
            public void onPrepared(String url, Object... objects)
            {
                isPrepared = true;
                super.onPrepared(url, objects);
            }

            @Override
            public void onAutoComplete(String url, Object... objects)
            {
                super.onAutoComplete(url, objects);
                binding.playVideo.startPlayLogic();
            }
        });
        if (playIndex == position)
            binding.playVideo.startPlayLogic();

        //缩略图
        GlideApp.with(mActivity)
                .load(videoInfoEntity.getThumbnails())
                .into(binding.videoCover);
        binding.tvNickName.setText("@" + videoInfoEntity.getNickName());
        binding.tvSimpleIntroduce.setText(videoInfoEntity.getContent());
        GlideApp.with(mActivity)
                .load(videoInfoEntity.getHeadPic())
                .circleCrop()
                .into(binding.ivHead);
        binding.tvFocus.setVisibility("0".equals(videoInfoEntity.getFollowOrNot()) ? View.VISIBLE : View.GONE);
        binding.tvLoveNum.setText(videoInfoEntity.getLikeNum());
        binding.tvCommentsNum.setText(videoInfoEntity.getCommentNum());
        binding.tvShareText.setText(videoInfoEntity.getShareNum());
        binding.rlComments.setOnClickListener(view -> {
            if (verticalViewPagerAdapterCallBack != null)
                verticalViewPagerAdapterCallBack.showCommentList(position, binding.tvCommentsNum);
        });
        binding.rlShare.setOnClickListener(view -> {
            if (verticalViewPagerAdapterCallBack != null)
                verticalViewPagerAdapterCallBack.showShareView(position, binding.tvShareText, binding.bottomSheetLayout);
        });
        binding.deleteVideo.setOnClickListener(view -> {
            if (verticalViewPagerAdapterCallBack != null)
                verticalViewPagerAdapterCallBack.deleteVideo(position);
        });
        binding.llBottomLay.setOnClickListener(view -> {
            if (verticalViewPagerAdapterCallBack != null)
                verticalViewPagerAdapterCallBack.showCommentDialog(position, binding.tvCommentsNum);
        });
        binding.ivHead.setOnClickListener(view -> {
            if (verticalViewPagerAdapterCallBack != null)
                verticalViewPagerAdapterCallBack.onUsernameOrHeaderClicked(position);
        });
        binding.tvNickName.setOnClickListener(view -> {
            if (verticalViewPagerAdapterCallBack != null)
                verticalViewPagerAdapterCallBack.onUsernameOrHeaderClicked(position);
        });
        binding.tvFocus.setOnClickListener(view -> {
            if (verticalViewPagerAdapterCallBack != null)
                verticalViewPagerAdapterCallBack.onFocusAction(position, binding.tvFocus);
        });

        //已点赞
        isLiked = "1".equals(videoInfoEntity.getLikeOrNot());
        binding.ivLove.setLiked(isLiked);

        if (SharePreUtils.getUId().equals(videoInfoEntity.getUserId()))
            binding.deleteVideo.setVisibility(View.VISIBLE);
        else
            binding.deleteVideo.setVisibility(View.GONE);
//            int res = "1".equals(videoInfo.getLikeOrNot()) ? R.drawable.but_big_heart_activate : R.drawable.but_big_heart_default;
//            ivLove.setTag("1".equals(videoInfo.getLikeOrNot()));
//            ivLove.setImageResource(res);


        binding.likeHeartView.setOnViewClickActionListener(new LikeHeartLayout.OnViewClickActionListener()
        {
            @Override
            public void onClick()
            {

                if (verticalViewPagerAdapterCallBack != null)
                    verticalViewPagerAdapterCallBack.videoClicked(position, binding.btnPlay);
            }

            @Override
            public void onDoubleClick()
            {
                if (verticalViewPagerAdapterCallBack != null)
                    verticalViewPagerAdapterCallBack.videoDoubleClicked(position, binding.ivLove, binding.tvLoveNum);
            }
        });


        binding.bottomSheetLayout.setPeekSheetTranslation(CommonUtils.dp2px(mActivity, 250));

        binding.bottomSheetLayout.addOnSheetDismissedListener(new OnSheetDismissedListener()
        {
            @Override
            public void onDismissed(BottomSheetLayout bottomSheetLayout)
            {
//                parentVerticalViewPager.setStopScroll(false);
            }
        });
        binding.bottomSheetLayout.removeOnSheetStateChangeListener(new BottomSheetLayout.OnSheetStateChangeListener()
        {
            @Override
            public void onSheetStateChanged(BottomSheetLayout.State state)
            {

            }
        });
        binding.ivLove.setOnLikeListener(new OnLikeListener()
        {
            @Override
            public void liked(LikeButton likeButton)
            {
                if (verticalViewPagerAdapterCallBack != null)
                    verticalViewPagerAdapterCallBack.likeButtonLiked(position, binding.ivLove, binding.tvLoveNum);
//                if (CommonUtils.checkUserStateFromFragment(VideoPlayFragment.this, TAG))
//                {
////                    isLiked = true;
////                    if (videoInfoEntity != null)
////                        model.startLikeVideo(videoInfoEntity.getVideoId());
//                }
//                else
//                    holder.ivLove.setLiked(false);
            }

            @Override
            public void unLiked(LikeButton likeButton)
            {
                if (verticalViewPagerAdapterCallBack != null)
                    verticalViewPagerAdapterCallBack.likeButtonUnLiked(position, binding.ivLove, binding.tvLoveNum);
//                isLiked = false;
//                if (videoInfoEntity != null)
//                    model.startLikeVideo(videoInfoEntity.getVideoId());
            }
        });
    }

    public void hideFocusView()
    {
        if (binding != null)
            binding.tvFocus.setVisibility(View.GONE);
    }

    public Map<Integer, Map<String, Object>> getVideoMap()
    {
        return videoMap;
    }

    public void setVerticalViewPagerAdapterCallBack(VerticalViewPagerAdapterCallBack verticalViewPagerAdapterCallBack)
    {
        this.verticalViewPagerAdapterCallBack = verticalViewPagerAdapterCallBack;
    }

//    public void hideBtnPlayImage()
//    {
//        if (binding != null)
//            binding.btnPlay.setVisibility(View.GONE);
//    }

    public interface VerticalViewPagerAdapterCallBack
    {

        void onUsernameOrHeaderClicked(int position);

        void videoClicked(int position, ImageView imgPlay);

        void videoDoubleClicked(int position, LikeButton likeButton, TextView tvLikeNum);

        void likeButtonLiked(int position, LikeButton likeButton, TextView tvLikeNum);

        void likeButtonUnLiked(int position, LikeButton likeButton, TextView tvLikeNum);

        void showCommentDialog(int position, TextView tvCommentNum);

        void showCommentList(int position, TextView tvCommentNum);

        void showShareView(int position, TextView tvShareNum, BottomSheetLayout bottomSheetLayout);

        void onFocusAction(int position, TextView tvFocusView);

        void deleteVideo(int position);

    }

}
